<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');

class Mr_pegawai extends CI_Controller {

	function __construct() {
    parent::__construct();
    $this->isLogin();	
		$this->load->model("master/master_pegawai"); 
		$this->load->model("master/master_pendidikan"); 		
		$this->load->model("master/master_agama"); 
		$this->num_post=10;				
	}

	function isLogin(){
		$login=$this->session->userdata('isLogin');
		if(! isset($login)||$login != true){
			$this->session->set_flashdata('error_login', 'Untuk Mengakses Admin Anda Harus Login!');
			redirect('login');	
		}
	}
	
	function index($offset = 0)
	{
		$data['main_view']='master/mr_pegawai/read';
		$data['aksi_cari']='mr_pegawai/cari_pegawai';
		$uri_segment = 3;
		
		if($offset != 0){
			$offset = $this->uri->segment($uri_segment);
		}else{
			$offset=$offset;
		}
		$data['list'] = $this->master_pegawai->limit($this->num_post, $offset);
		$data['offset']=$offset;
		$num_rows = $this->master_pegawai->num_rows();
			
		if($num_rows>0){
			// Membuat pagination
			$config['base_url'] = base_url().'mr_pegawai/index';
			$config['cur_tag_open'] = '<li class=active><a href="#">';
			$config['cur_tag_close'] = '<span class="sr-only">(current)</span></a></li>';
			$config['num_tag_open'] = '<li>';
			$config['num_tag_close'] = '</li>';
			$config['prev_link'] = '<';
			$config['prev_tag_open'] = '<li>';
			$config['prev_tag_close'] = '</li>';
			$config['next_link'] = '>';
			$config['next_tag_open'] = '<li>';
			$config['next_tag_close'] = '</li>';
			//buat last
			$config['last_tag_open'] = '<li>';
			$config['last_tag_close'] = '</li>';
			$config['last_link'] = '>>';
			//buat first
			$config['first_tag_open'] = '<li>';
			$config['first_tag_close'] = '</li>';
			$config['first_link'] = '<<';
			$config['total_rows'] = $num_rows;
			$config['per_page'] = $this->num_post;
			$config['uri_segment'] = $uri_segment;
			$this->pagination->initialize($config);
			$data['pagination'] = $this->pagination->create_links();
		}
		$this->load->view('index',$data);
		
	}	

	function cari_pegawai($offset = 0)
	{
		$data['main_view']='master/mr_pegawai/read';
		$data['aksi_cari']='mr_pegawai/cari_pegawai';
		$keyword = $this->input->post('judul');

		$uri_segment = 3;
		
		if($offset != 0){
			$offset = $this->uri->segment($uri_segment);
		}else{
			$offset=$offset;
		}
		//buat cari
		if(isset($_POST['cari'])){
			$data['param_judul'] = $this->input->post('judul');
			$this->session->set_userdata('param_judul', $data['param_judul']);
		} else {
			$data['param_judul'] = $this->session->userdata('param_judul');
		}
		
		$data['list'] = $this->master_pegawai->limit_search($data['param_judul'],$this->num_post, $offset);
		$data['offset']=$offset;
		$num_rows = $this->master_pegawai->num_rows_search($data['param_judul']);
			
		if($num_rows>0){
			// Membuat pagination
			$config['base_url'] = base_url().'mr_pegawai/cari_pegawai';
			$config['cur_tag_open'] = '<li class=active><a href="#">';
			$config['cur_tag_close'] = '<span class="sr-only">(current)</span></a></li>';
			$config['num_tag_open'] = '<li>';
			$config['num_tag_close'] = '</li>';
			$config['prev_link'] = '<';
			$config['prev_tag_open'] = '<li>';
			$config['prev_tag_close'] = '</li>';
			$config['next_link'] = '>';
			$config['next_tag_open'] = '<li>';
			$config['next_tag_close'] = '</li>';
			//buat last
			$config['last_tag_open'] = '<li>';
			$config['last_tag_close'] = '</li>';
			$config['last_link'] = '>>';
			//buat first
			$config['first_tag_open'] = '<li>';
			$config['first_tag_close'] = '</li>';
			$config['first_link'] = '<<';
			$config['total_rows'] = $num_rows;
			$config['per_page'] = $this->num_post;
			$config['uri_segment'] = $uri_segment;
			$this->pagination->initialize($config);
			$data['pagination'] = $this->pagination->create_links();
		}
		$this->load->view('index',$data);
		
	}
	
	function add()
	{
		$data['main_view']='master/mr_pegawai/add';
		$data['list_p'] = $this->master_pendidikan->read();
		$data['list_a'] = $this->master_agama->read();
		$this->load->view('index',$data);
	}
	
	function add_exe()
	{
		$tgl = $this->input->post('tgl_lahir');
		$tglMySQL = date("Y-m-d", strtotime ($tgl));
		$tglMasuk = date("Y-m-d", strtotime ($this->input->post('tgl_masuk')));
		$max = $this->master_pegawai->nip();
		$n1 = substr("$tglMasuk", -2);
		$n2 = substr("$tglMasuk", -5, 2);
		$n3 = substr("$tglMasuk", 2, 2);
		$idMax = $max['nip'];
		$noUrut = (int) $idMax;
		$noUrut++;
		$nipMax = sprintf("%03s", $noUrut);
		$nip = "$n1$n2"."-"."$n3"."-"."$nipMax";

		$dataInsert = array(
			'nip' => $nip,
			'nm_pegawai' => $this->input->post('nm_pegawai'),
			'tgl_masuk' => $tglMasuk,
			'jk' => $this->input->post('jk'),
			'tmp_lahir' => $this->input->post('tmp_lahir'),
			'tgl_lahir' => $tglMySQL,
			'ijazah_akhir' => $this->input->post('ijazah_akhir'),
			'id_mr_agama' => $this->input->post('id_mr_agama'),
			'tlp' => $this->input->post('tlp'),
			'email' => $this->input->post('email'),
			'alamat' => $this->input->post('alamat')
		);
		$this->master_pegawai->add($dataInsert);		
		redirect ('mr_pegawai');
	}	
	
	function edit()
	{
		$id_mr_pegawai = $this->security->xss_clean($this->uri->segment(3));	
		$data['list']=$this->master_pegawai->edit($id_mr_pegawai);	
		$data['list_p']=$this->master_pendidikan->read();		
		$data['list_a']=$this->master_agama->read();				
		$data['main_view']='master/mr_pegawai/edit';
		$this->load->view('index',$data);
	}
	
	function update()
	{
		$id_mr_pegawai=$this->input->post('id_mr_pegawai');
		
		$tgl = $this->input->post('tgl_lahir');
		$tglMySQL = date("Y-m-d", strtotime ($tgl));
		
		$dataUpdate=array(
			'nm_pegawai' => $this->input->post('nm_pegawai'),
			'jk' => $this->input->post('jk'),
			'tmp_lahir' => $this->input->post('tmp_lahir'),
			'tgl_lahir' => $tglMySQL,
			'ijazah_akhir' => $this->input->post('ijazah_akhir'),
			'id_mr_agama' => $this->input->post('id_mr_agama'),
			'tlp' => $this->input->post('tlp'),
			'email' => $this->input->post('email'),
			'alamat' => $this->input->post('alamat')
		);
		$this->master_pegawai->update($id_mr_pegawai,$dataUpdate);
		redirect('mr_pegawai');
	}	
	
	function delete()
	{
		$id_mr_pegawai = $this->security->xss_clean($this->uri->segment(3));
		$this->master_pegawai->delete($id_mr_pegawai);
		redirect('mr_pegawai');
	}
	
	function detail()
	{
		$id_mr_pegawai=$this->input->post('id_mr_pegawai');
		$data['list']=$this->master_pegawai->detail($id_mr_pegawai);	
		$this->load->view('master/mr_pegawai/detail',$data);
	}
	
}